<?php

namespace App\Library\Logic;

use App\Library\Lock;
use App\Library\Requests\Base\BasePageRequest;
use App\Models\InviteModel;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Collection;

class InviteLogic extends InviteModel
{
    public static function getInviteeInfo(int $userId): Model|Builder|null
    {
        return self::query()->where('invitee_id', $userId)->first();
    }

    /**
     *
     * @param BasePageRequest $request
     * @return array
     *
     */
    public function list(BasePageRequest $request): array
    {
        return paginate(self::query()
            ->leftJoin('shop_user', 'shop_user.user_id', 'invitee_id')
            ->select([
                'inviter_id',
                'commission',
                'order_count',
                'ratio',
                'level',
                'shop_user.user_id',
                'user_name',
                'user_mobile'
            ])
            ->where('inviter_id', USERID)->paginate($request->limit));
    }

    public function mineUpper()
    {
        return self::query()
            ->leftJoin('shop_user', 'shop_user.user_id', 'inviter_id')
            ->where([
                'invitee_id' => USERID,
            ])->value('user_name');
    }
}
